package com.mts.javacommon.config;

import java.util.List;
import lombok.Data;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/** <pre> 系统配置 </pre> */
@Configuration
@ConfigurationProperties(prefix = "application", ignoreInvalidFields = true)
public class ApplicationProperties {


    @Getter
    private final JwtConfig jwt = new JwtConfig();

    @Getter
    private final AuthConfig authConfig = new AuthConfig();

    @Getter
    private final Swagger2 swagger2 = new Swagger2();

    @Getter
    private final MailConfig mail = new MailConfig();

    @Getter
    private final LoginLimit loginLimit = new LoginLimit();

    /** <pre> 登录限制 </pre> */
    @Data
    public static class LoginLimit {

        /** <pre> minutes 分钟内，登录失败 errorTimes 次 </pre> */
        private int minutes = 60;

        /** <pre> minutes 分钟内，登录失败 errorTimes 次 </pre> */
        private int errorTimes = 5;

        /** <pre> 默认1800秒内没有请求，那么就算用户不在线 </pre> */
        private int onlineWindowSeconds = 1800;
    }

    /** <pre> 发送邮件的配置 </pre> */
    @Data
    public static class MailConfig {
        private boolean auth = true;
        private String host;
        private int port = 25;
        private String from;
        private String user;
        private String pass;
        private boolean ssl = false;
        private long timeoutMs = 5000;
    }

    @Data
    public static class Swagger2 {
        /** <pre> 是否启用Swagger2 </pre> */
        private boolean enable;

        /** <pre> Swagger2文档显示的标题 </pre> */
        private String title;

        /** <pre> Swagger2文档显示的描述 </pre> */
        private String description;

        /** <pre> Swagger2文档API版本号 </pre> */
        private String version;

        /** <pre> Swagger2相关的URL，在启用Swagger时需要配置入权限白名单 </pre> */
        private List<String> urls;

        /** <pre> 联系人 </pre> */
        @Getter
        private final Contact contact = new Contact();

        @Data
        public static class Contact {
            private String name;
            private String url;
            private String email;
        }
    }

    /** <pre> 授权相关的配置 </pre> */
    @Data
    public static class AuthConfig {
        /** <pre> 不做权限校验的白名单 </pre> */
        private List<String> whites;

        /** <pre> 是否从Swagger中读取Api信息，并更新到数据库中 </pre> */
        private boolean updateApis = false;
    }

    /** <pre> jwt配置 </pre> */
    @Data
    public static class JwtConfig {
        private String secret;

        private long tokenValidityInSeconds;

        private long tokenValidityInSecondsForRememberMe;
    }
}
